package com.hp.easyBuy.utils;

import com.hp.easyBuy.contants.StatusCode;

import java.io.Serializable;

/*
* 响应统一体，用于统一响应格式
* */
public class R <T> implements Serializable {

    private Boolean flag;   //响应是否成功
    private Integer code;   //响应编码
    private String message; //响应信息


    private T data;        //响应数据

    public R(Boolean flag, Integer code, String message, T data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //什么都不传，默认响应成功
    public R() {
        this.flag = true;
        this.code = StatusCode.SUCCESS;
        this.message = "操作成功";
    }

    public R(Boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;

    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    //什么都不返回默认是成功
    public static <E>  R<E> success(){
        R<E> r = new R<E>(true, StatusCode.SUCCESS, "操作成功");
        return r;
    }

    public static <E>  R<E> success(Integer code , String message){
        R<E> r = new R<E>(true, code, message);
        return r;
    }

    public static <E>  R<E> error(){
        R<E> r = new R<E>(false, StatusCode.ERROR1, "操作失败");
        return r;
    }

    public static <E>  R<E> error(Integer code , String message){
        R<E> r = new R<E>(false, code, message);
        return r;
    }

    //只返回数据也默认成功
    public static <E>  R<E> success(E e){
        R<E> r = new R<E>(true, StatusCode.SUCCESS, "操作成功",e);
        return r;
    }

    //返回全部
    public static <E>  R<E> success(Boolean flag, Integer code, String message, E data){
        R<E> r = new R<E>(flag, code, message,data);
        return r;
    }

    //返回Boolean flag, Integer code, String message，表示不返回数据的结果
    public static <E>  R<E> success(Boolean flag, Integer code, String message){
        R<E> r = new R<E>(flag, code, message);
        return r;
    }

}
